package com.cuixichang.minimalism.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuixichang.minimalism.core.mybatis.cnstants.BaseFieldNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import com.cuixichang.minimalism.mapper.IFormValueMapper;
import com.cuixichang.minimalism.transfer.dto.IFormValueDTO;
import com.cuixichang.minimalism.transfer.factory.IFormValueAssembler;
import com.cuixichang.minimalism.transfer.po.IFormValue;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.function.Function;
import org.springframework.stereotype.Service;

/**
 * 表服务实现类
 *
 * @author cuixichang
 * @date 2024-12-20
 */
@Service
public class IFormValueServiceImpl implements IFormValueService {

    @Autowired
    private IFormValueMapper iFormValueMapper;

    @Override
    public IFormValueDTO selectById(Long id){
        IFormValue iFormValue = iFormValueMapper.selectById(id);
        return IFormValueAssembler.INSTANCT.convertDto(iFormValue);
    }

    @Override
    public IFormValueDTO selectOne(IFormValueDTO iFormValueDTO){
        Assert.notNull(iFormValueDTO,"参数为空,请检查");
        IFormValue iFormValue = IFormValueAssembler.INSTANCT.convertPo(iFormValueDTO);
        IFormValue result = iFormValueMapper.selectOne(new QueryWrapper<>(iFormValue));
        return IFormValueAssembler.INSTANCT.convertDto(result);
    }

    @Override
    public List<IFormValueDTO> selectByIds(List<Long> ids) {
        Assert.notEmpty(ids,"参数为空,请检查");
        List<IFormValue> results = iFormValueMapper.selectBatchIds(ids);
        return results.stream().map(IFormValueAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public List<IFormValueDTO> selectList(IFormValueDTO iFormValueDTO){
        return this.selectList(iFormValueDTO, (iFormValue, queryWrapper) -> {});
    }

    @Override
    public List<IFormValueDTO> selectList(IFormValueDTO iFormValueDTO, BiConsumer<IFormValue, QueryWrapper<IFormValue>> consumer){
        Assert.notNull(iFormValueDTO,"参数为空,请检查");
        IFormValue iFormValue = IFormValueAssembler.INSTANCT.convertPo(iFormValueDTO);
        QueryWrapper<IFormValue> queryWrapper = new QueryWrapper<>(iFormValue);
        if(Objects.nonNull(consumer)){
            consumer.accept(iFormValue, queryWrapper);
        }
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        List<IFormValue> results = iFormValueMapper.selectList(queryWrapper);
        return results.stream().map(IFormValueAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public Page<IFormValueDTO> selectPage(IFormValueDTO iFormValueDTO,long pageSize,long pageNo){
        return this.selectPage(iFormValueDTO, pageSize, pageNo, (iFormValue, queryWrapper) -> {});
    }

    @Override
    public Page<IFormValueDTO> selectPage(IFormValueDTO iFormValueDTO,long pageSize,long pageNo, BiConsumer<IFormValue, QueryWrapper<IFormValue>> consumer){
        Assert.notNull(iFormValueDTO,"参数为空,请检查");
        IFormValue iFormValue = IFormValueAssembler.INSTANCT.convertPo(iFormValueDTO);
        QueryWrapper<IFormValue> queryWrapper = new QueryWrapper<>(iFormValue);
        if(Objects.nonNull(consumer)) consumer.accept(iFormValue, queryWrapper);
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        Page<IFormValue> page = iFormValueMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper);
        Page<IFormValueDTO> result = Page.of(page.getCurrent(),page.getSize(),page.getTotal());
        result.setRecords(page.getRecords().stream()
                .map(IFormValueAssembler.INSTANCT::convertDto).collect(Collectors.toList()));
        return result;
    }

    @Override
    public int deleteById(Long id){
        return iFormValueMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<Long> ids){
        return iFormValueMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteByCondition(IFormValueDTO iFormValueDTO){
        return deleteByCondition(iFormValueDTO, (iFormValue, queryWrapper)-> {});
    }

    @Override
    public int deleteByCondition(IFormValueDTO iFormValueDTO, BiConsumer<IFormValue, QueryWrapper<IFormValue>> consumer){
        Assert.notNull(iFormValueDTO,"参数为空,请检查");
        IFormValue iFormValue = IFormValueAssembler.INSTANCT.convertPo(iFormValueDTO);
        QueryWrapper<IFormValue> queryWrapper = new QueryWrapper<>(iFormValue);
        if(Objects.nonNull(consumer)) {
            consumer.accept(iFormValue, queryWrapper);
        }
        return iFormValueMapper.delete(queryWrapper);
    }

    @Override
    public IFormValueDTO insert(IFormValueDTO iFormValueDTO){
        return insertHasOption(iFormValueDTO, (IFormValueDTO) -> null);
    }

    @Override
    public IFormValueDTO insertHasOption(IFormValueDTO iFormValueDTO, Function<IFormValueDTO,IFormValueDTO> preOption){
        Assert.notNull(iFormValueDTO,"参数为空,请检查");
        iFormValueDTO.check();
        IFormValue iFormValue = IFormValueAssembler.INSTANCT.convertPo(iFormValueDTO);
        Long createLoginId = iFormValue.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iFormValue.setUpdateLoginId(createLoginId);
        LocalDateTime localDateTime = LocalDateTime.now();
        iFormValue.setCreateTime(localDateTime);
        iFormValue.setUpdateTime(localDateTime);
        Boolean isEnable = iFormValue.getIsEnable();
        if(Objects.isNull(isEnable)) iFormValue.setIsEnable(true);
        Integer sort = iFormValue.getSort();
        if(Objects.isNull(sort)) iFormValue.setSort(0);
        if(Objects.nonNull(preOption)){
            IFormValueDTO apply = preOption.apply(iFormValueDTO);
            if(Objects.nonNull(apply)) {
              return apply;
            }
        }
        int insert = iFormValueMapper.insert(iFormValue);
        Assert.isTrue(insert>0,"插入失败");
        return IFormValueAssembler.INSTANCT.convertDto(iFormValue);
    }

    @Override
    public int updateById(IFormValueDTO iFormValueDTO){
         return updateById(iFormValueDTO, (iFormValue, updateWrapper) -> {});
    }

    @Override
    public int updateById(IFormValueDTO iFormValueDTO, BiConsumer<IFormValue, UpdateWrapper<IFormValue>> consumer){
        Assert.notNull(iFormValueDTO,"参数为空,请检查");
        Long id = iFormValueDTO.getId();
        Assert.notNull(id,"主键为空,请检查");
        IFormValue iFormValue = IFormValueAssembler.INSTANCT.convertPo(iFormValueDTO);
        Long createLoginId = iFormValue.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iFormValue.setCreateLoginId(null);
        iFormValue.setUpdateLoginId(createLoginId);
        iFormValue.setUpdateTime(LocalDateTime.now());
        UpdateWrapper<IFormValue> updateWrapper = new UpdateWrapper<IFormValue>().eq(BaseFieldNames.ID, id);
        if(Objects.nonNull(consumer)) consumer.accept(iFormValue,updateWrapper);
        return iFormValueMapper.update(iFormValue,updateWrapper);
    }
}
